/*
 * Copyright 2009, Qys Creations 
 * 
 *  This file is part of Chameleon DB.
 *
 *  Chameleon DB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chameleon DB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chameleon DB.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */


import java.util.*;
public class db_result_dataset implements db_interface_result{
	private int iRowCnt = 0;
	private ArrayList<db_column> oCols = null;
	private byte[] bData = null;
	
	db_result_dataset (ArrayList<db_column> oColumns, int iRowCount, byte[] bDataSet) {
		oCols = oColumns;
		iRowCnt = iRowCount; 
		bData = bDataSet;
	}
	public byte[] getBytes() {
		// 4 byte for number of columns. 
		// Then 4 + 4 + 4 = 12 for col type, length of type, and length of the column name 
		// Then 4 for number of rows and finally length of the row data
		int iSize = 4 + 4 + oCols.size() * (12) + 4 + bData.length;
		for (int i = 0; i < oCols.size(); i++)
			iSize += oCols.get(i).sName.length();
		byte[] bHeaders = ("200 OK QDBP/1.0\nlength: " + iSize + "\n\n").getBytes();
		db_bytes oOut = new db_bytes(iSize + bHeaders.length);

		oOut.write(bHeaders);
		oOut.write(db_util.DB_RESULT_DATASET);
		oOut.write(oCols.size());
		
		for (int i = 0; i < oCols.size(); i++) {
			
			oOut.write((int)oCols.get(i).lType);
			oOut.write((int)oCols.get(i).lLength);
			oOut.write(oCols.get(i).sName);
		}

		

		oOut.write(iRowCnt);
		oOut.write(bData);
	
		return oOut.getBytes(); 
	}
}
